package uit.com.project.iCampus.dao.hibernate;

import java.util.List;

import org.hibernate.HibernateException;

import uit.com.project.iCampus.dao.UserOnlineDAO;
import uit.com.project.iCampus.model.UserOnline;

public class UserOnlinerDAOHibernate extends GenericHibernateDAO<UserOnline, Long> implements UserOnlineDAO{

	@SuppressWarnings("unchecked")
	@Override
	public List<UserOnline> getAllUserOnlines() {
		return getSession().createCriteria(UserOnline.class).list();
	}

	@Override
	public boolean isOnline(String username) {
		@SuppressWarnings("unchecked")
		List<UserOnline> list = getSession()
				.createQuery(
						"FROM UserOnline AS UserOnline WHERE UserOnline.user.username = :username")
				.setString("username", username)
				.list();
				
		if (list != null && list.size() > 0)
			return true;
		else 
			return false;
	}

	@Override
	public boolean deleteUserOnline(String username) {
		try {
			@SuppressWarnings("unchecked")
			List<UserOnline> list = getSession()
					.createQuery(
							"FROM UserOnline AS UserOnline WHERE UserOnline.user.username = :username")
					.setString("username", username)
					.list();
			if (list.size() > 0) {
				getSession().delete(list.get(0));
				return true;
			}
		} catch (HibernateException e) {
			e.printStackTrace();
			return false;
		}
		return false;
		
	}
}